(function ($) {
$.fn.extend({
    //插件名称
    page: function (options) {

        //参数和默认值
        var defaults = {
            currentPage:0,
            pageSize:0,
            totalRecords:0,
            pageNumberNormalClassName:"paginate_button",
            pageNumberCurrentClassName:"paginate_active",
            numberOnclick:function (pageNumber) {},
            submitDataConfig: {
                formId:"", //如果设置了formId，那么在页选项被点击时，提交此form
                pageNumberInputName:"pageNum", //pageNumber
                pageSizeInputName:""
            }
        };

        var options = $.extend(defaults, options);


        return this.each(function () {
            var $pageWrapper = $(this);
			var page = {
				init : function () {
                    var html = '';
                    var pageWrapperId = $pageWrapper.attr("id");
                    var pagePanId = pageWrapperId + "PagePan";
                    var statisticsId = pageWrapperId + "total";
                    var pageNumberId = pageWrapperId + "pageNumber";
                    html += '<div id="' + pagePanId + '" class="fg-toolbar tableFooter">';
                    html += '	<div id="' + statisticsId + '" class="dataTables_info"></div>';
                    html += '	<div class="dataTables_paginate paging_full_numbers">';
                    html += '		<span id="' + pageNumberId + '"></span>';
                    html += '	</div>';
                    html += '</div>';

                    $pageWrapper.append($(html));

                    var totalPage = options.totalRecords%options.pageSize > 0 && parseInt(options.totalRecords/options.pageSize) + 1 || options.totalRecords/options.pageSize;
                    if(totalPage==0){
                        $("#"+pagePanId).hide();
                    }else{
                        $("#"+pagePanId).show();
                    }

                    if(totalPage>0) {
                        var firstBtnId = pageWrapperId + "first";
                        var lastBtnId = pageWrapperId + "last";
                        var pageNumberHtml = '<a id="'+firstBtnId+'" class="first paginate_button" pageNum="1">首页</a>';
                        var endHtml = '<a class="last paginate_button" pageNum="' + totalPage + '" id="'+lastBtnId+'">末页</a>';
                        var start = options.currentPage - 2;
                        var end = options.currentPage + 2;
                        if (start < 1) {
                            start = 1;
                            if (options.currentPage == 2) {
                                end += 1;
                            } else {
                                end += 2;
                            }
                        }
                        if (end > totalPage) {
                            end = totalPage;
                            if (start > 1) {
                                if (options.currentPage == totalPage - 1) {
                                    start -= 1;
                                } else {
                                    start -= 2;
                                }
                            }
                        }
                        if (start < 1) {
                            start = 1;
                        }
                        for (var i = start; i <= end; i++) {
                            if (i == options.currentPage) {
                                pageNumberHtml += '<a pageNum="' + i + '" class="'+options.pageNumberCurrentClassName+'">' + i + '</a>';
                            } else {
                                pageNumberHtml += '<a pageNum="' + i + '" class="'+options.pageNumberNormalClassName+'">' + i + '</a>';
                            }
                        }
                        pageNumberHtml += endHtml;
                        $("#" + pageNumberId).html("");
                        $("#" + pageNumberId).append(pageNumberHtml);

                        var recordBegin = (options.currentPage - 1) * options.pageSize + 1;
                        var recordEnd;
                        if (options.totalRecords - recordBegin < options.pageSize) {
                            recordEnd = options.totalRecords;
                        } else {
                            recordEnd = recordBegin + options.pageSize -1;
                        }
                        $("#" + statisticsId).html(" 当前显&nbsp;示&nbsp;"+recordBegin+"到&nbsp;"+recordEnd+"&nbsp;&nbsp; 共 "+options.totalRecords+" 个条目 "+totalPage+" 页");

                        $("#" + pageNumberId + " a").click(function () {
                            options.numberOnclick($(this).attr("pageNum"));
                        });

                        var submitDataConfig = options.submitDataConfig;
                        if (submitDataConfig.formId != "") {
                            $("#" + pageNumberId + " a").click(function () {
                                if ($("#" + submitDataConfig.formId + " [name="+submitDataConfig.pageNumberInputName+"]").get(0)) {
                                    $("#" + submitDataConfig.formId + " [name="+submitDataConfig.pageNumberInputName+"]").val($(this).attr("pageNum"));
                                } else {
                                    $("#" + submitDataConfig.formId).append($('<input type="hidden" id="'+submitDataConfig.formId+submitDataConfig.pageNumberInputName+'" value="'+$(this).attr("pageNum")+'" name="'+submitDataConfig.pageNumberInputName+'">'));
                                }
                                if ($("#" + submitDataConfig.formId).find("[name="+submitDataConfig.pageSizeInputName+"]").get(0)) {
                                    $("#" + submitDataConfig.formId + " [name="+submitDataConfig.pageSizeInputName+"]").val(options.pageSize);
                                } else {
                                    $("#" + submitDataConfig.formId).append($('<input type="hidden" id="'+submitDataConfig.formId+submitDataConfig.pageSizeInputName+'" value="'+options.pageSize+'" name="'+submitDataConfig.pageSizeInputName+'">'));
                                }
                                $("#" + submitDataConfig.formId).submit();
                            });
                        }
                    }
				}
			};

            page.init();

        });
    }
    });})(jQuery);